<template>
    <el-card class="avatar-card" shadow="hover">
        <div class="avatar-title">
            <h3>更换头像</h3>
        </div>
        <div class="avatar-section">
            <el-upload class="avatar-uploader" action="#" :show-file-list="false" :http-request="handleUpload1"
                :before-upload="addAvatarUpload">
                <img v-if="imageUpdateUrl" :src="imageUpdateUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                    <Plus />
                </el-icon>
            </el-upload>
            <el-button type="success" @click="subUpate">点击上传新头像</el-button>
        </div>
    </el-card>
</template>

<script setup>
import userApi from "@/api/userApi";
import fileApi from "@/api/fileApi";
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

// 用户信息
const userInfo = ref({});
// 更新头像地址
const imageUpdateUrl = ref('');



// 获得用户信息
const getInfo = () => {
    let id = sessionStorage.getItem("userInfoUserId")
    userApi.getById(id)
        .then(resp => {
            if (resp.code === 10000) {
                userInfo.value = resp.data;
                imageUpdateUrl.value = 'http://localhost:8080/images/' + resp.data.userAvatar;
            }
        });
}

// 新增之前的钩子函数
const addAvatarUpload = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('图片格式要是JPG')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('图片大小不要超过2MB!')
        return false
    }
    return true
}
// 上传图片方法
const handleUpload1 = (file) => {
    let fd = new FormData();
    fd.append("file", file.file);
    fileApi.upload(fd)
        .then(resp => {
            if (resp.code === 10000) {
                imageUpdateUrl.value = 'http://localhost:8080/images/' + resp.data;
                userInfo.value.userAvatar = resp.data;
            }
        });
}

// 提交修改
const subUpate = () => {
    userApi.updateInfo(userInfo.value)
        .then(resp => {
            if (resp.code === 10000) {
                ElMessage.success(resp.msg);
                getInfo();
            } else {
                ElMessage.error(resp.msg);
                // getInfo();
            }
        });
}

getInfo();
</script>

<style scoped>
.avatar-card {
    width: 400px;
    margin: 0 auto;
    padding: 30px;
    text-align: center;
}

.avatar-title {
    margin-bottom: 20px;
}

.avatar-section {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>


<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>